package org.zoomdev.zoom.dao.adapters.parameter;

import org.zoomdev.zoom.common.caster.ValueCaster;
import org.zoomdev.zoom.dao.adapters.ParameterAdapter;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 比如Map到clob
 */
public class CasterProxyStatementAdapter implements ParameterAdapter {
    private final ValueCaster valueCaster;
    private final ParameterAdapter statementAdapter;

    public CasterProxyStatementAdapter(ValueCaster valueCaster, ParameterAdapter statementAdapter) {
        this.valueCaster = valueCaster;
        this.statementAdapter = statementAdapter;
    }


    @Override
    public void setNonnullParameter(PreparedStatement ps, int index, Object data) throws SQLException {
        Object value = valueCaster.to(data);
        statementAdapter.setNonnullParameter(ps, index, value);
    }
}
